<?php

namespace App\Api\Repositories\User;

use App\Models\Option;
use App\Models\User\UserDetail as Model;
use App\Models\User\Users;

use Illuminate\Support\Facades\Cache;

class UserDetailRepositories
{
    protected $eloquentClass = Model::class;

    /**
     * 创建数据
     *
     * @param int $uid 会员id
     * @return void
     */
    public function create_data($uid)
    {
        return $this->eloquentClass::create([
            'id' => $uid,
        ]);
    }

    /**
     * 传入要修改的字段与值，修改
     *
     * @param int $uid 会员id
     * @param array $data 要修改的数据
     * @return void
     */
    public function update_data($uid, $data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s", time());
        return $this->eloquentClass::where('id', $uid)->update($data);
    }

    /**
     * 根据id获取会员详情信息
     *
     * @param int $uid
     * @return void
     */
    public function use_id_get_data($uid)
    {
        $data = $this->eloquentClass::where('id', $uid)->first();
        if (isset($data->users)) {
            $data->age = $data->age;
            $data->nickname = $data->users->nickname;
            $data->avatar = $data->users->avatar;
            unset($data->users);
            return $data;
        } else {
            return false;
        }
    }

    /**
     * 内部信息搜索
     *
     * @param string $search
     * @return void
     */
    public function select_interior_data($search)
    {
        $obj = $this->eloquentClass::where('sex', $search['sex']);
        $obj = $search['city'] == '' ? $obj : $obj->where('city', 'like', $search['city']);
        $obj = $search['birthday'][0] == '*' ? $obj : $obj->whereIn('birthday', $search['birthday']);
        $obj = $search['height'] == '' ? $obj : $obj->whereIn('height', $search['height']);
        $obj = $search['face_score'] == '' ? $obj : $obj->where('face_score', $search['face_score']);
        $obj = $search['family'] == '' ? $obj : $obj->where('family', $search['family']);
        $obj = $search['income'] == '' ? $obj : $obj->where('income', $search['income']);
        $obj = $search['constellation'] == '' ? $obj : $obj->where('constellation', $search['constellation']);
        $obj = $search['carandhouse'] == '' ? $obj : $obj->where('car_and_house', $search['carandhouse']);
//        $obj = $search['college_tab'] == '' ? $obj : $obj->where('college_tab', $search['college_tab']);
        if ($search['college_tab'] != '') {
            $college = explode(",", $search['college_tab']);
            if (is_array($college)) {
                $options = [];
                foreach ($college as $value) {
                    if ($value != "") {
                        $options[] = Option::where('map_ids', 'like', "%" . $value . "%")->pluck('option');
//                        if (empty($options)) {
//                            $options = Option::where('map_ids', 'like', "%" . $value . "%")->pluck('option');
//                        } else {
//                            $options = array_merge($options, Option::where('map_ids', 'like', "%" . $value . "%")->pluck('option'));
//                        }
                    }
                }
            } else {
                $options = Option::where('map_ids', 'like', "%" . $search['college_tab'] . "%")->pluck('option');
            }
            $obj = $obj->whereIn('college', $options);
        }
        foreach ($search['keyword'] as $v) {
            $obj = $obj->where('keyword', 'like', '%' . $v . '%');
        }
        $res = $obj->limit(5)->get();
        $user_repositories = new \App\Api\Repositories\User\UsersRepositories();
        foreach ($res as &$v) {
            $user = $user_repositories->get_data('id', $v->id, ['nickname', 'avatar']);
//            if ($user) {
            $v->nickname = $user->nickname;
            $v->avatar = $user->avatar;
//            }
        }
        return $res;
    }

    /**
     * 获取男性女性数量
     *
     * @return void
     */
    public function get_count_diff_sex()
    {
        $woman_count = Cache::remember('user_woman_count', 60, function () {
            return $this->eloquentClass::where('sex', '女')->count();
        });
        $man_count = Cache::remember('user_man_count', 60, function () {
            return $this->eloquentClass::where('sex', '男')->count();
        });
        return ['woman_count' => $woman_count, 'man_count' => $man_count];
    }
}
